//
// Copyright (c) 2016 Related Code - http://relatedcode.com
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "utilities.h"

@implementation Location

//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (Location *)shared
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
	static dispatch_once_t once;
	static Location *location;
	//---------------------------------------------------------------------------------------------------------------------------------------------
	dispatch_once(&once, ^{ location = [[Location alloc] init]; });
	//---------------------------------------------------------------------------------------------------------------------------------------------
	return location;
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)start
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
	[[self shared].locationManager startUpdatingLocation];
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (void)stop
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
	[[self shared].locationManager stopUpdatingLocation];
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (CLLocationDegrees)latitude
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
	return [self shared].coordinate.latitude;
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
+ (CLLocationDegrees)longitude
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
	return [self shared].coordinate.longitude;
}

#pragma mark - Instance methods

//-------------------------------------------------------------------------------------------------------------------------------------------------
- (id)init
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
	self = [super init];
	//---------------------------------------------------------------------------------------------------------------------------------------------
	self.locationManager = [[CLLocationManager alloc] init];
	[self.locationManager setDelegate:self];
	[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
	[self.locationManager requestWhenInUseAuthorization];
	//---------------------------------------------------------------------------------------------------------------------------------------------
	return self;
}

#pragma mark - CLLocationManagerDelegate

//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
	self.coordinate = newLocation.coordinate;
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
	
}

@end

